#ifndef STATUSNOTIFIERMODEL_H
#define STATUSNOTIFIERMODEL_H

#include <QAbstractListModel>
#include <QMenu>
#include <QSettings>
#include <QSortFilterProxyModel>

#include "status-notifier-item.h"
#include "tray-icon-item.h"

namespace UkuiSystemTray {
class GroupModel;
class TrayIconItemsModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum Role {
        Service,
        AttentionIcon,
        AttentionIconName,
        AttentionMovieName,
        Category,
        Icon,
        IconName,
        IconThemePath,
        Id,
        ItemIsMenu,
        OverlayIcon,
        OverlayIconName,
        Status,
        Title,
        ToolTipSubTitle,
        ToolTipTitle,
        WindowId,
        Fixed,
        RecordOrder,
        Row
    };

    Q_ENUM(Role)

    explicit TrayIconItemsModel(QObject *parent = nullptr);

    int rowCount(const QModelIndex &parent) const override;
    QVariant data(const QModelIndex &index, int role) const override;
    QHash<int, QByteArray> roleNames() const override;
    Q_INVOKABLE QModelIndex index(int row, int column, const QModelIndex &parent) const override;\
    Q_INVOKABLE void setOrderInGroup(UkuiSystemTray::GroupModel *group, const QModelIndex &groupIndex, int order);
    Q_INVOKABLE void setOrderBetweenGroups(UkuiSystemTray::GroupModel *fromGroup, const QModelIndex &beginIndex, UkuiSystemTray::GroupModel *toGroup, int order);
    Q_INVOKABLE void setGroupBegin(UkuiSystemTray::GroupModel *group, int begin);
    Q_INVOKABLE void setGroupEnd(UkuiSystemTray::GroupModel *group, int end);

public Q_SLOTS:
    void setOrder(const QModelIndex &index, int order);
    void activate(const QModelIndex &index);
    void showContextMenu(const QModelIndex &index);

private Q_SLOTS:
    void addSource(const QString &source);
    void removeSource(const QString &source);
    void dataUpdated(const QString &sourceName);
    void showMenu(QMenu *menu);

private:
    int indexOfSource(const QString &source) const;

    QVector<TrayIconItem *> m_item;
    QSettings *m_settings = nullptr;
    QStringList m_order;
    QStringList m_fixedItems;
};
class GroupModel : public QSortFilterProxyModel
{
   Q_OBJECT
public:
    explicit GroupModel(QObject *parent = nullptr);
    Q_INVOKABLE void setSourceModel(QAbstractItemModel *sourceModel) override;
    QHash<int, QByteArray> roleNames() const override;
    QVariant data(const QModelIndex &index, int role) const override;

    Q_INVOKABLE void setGroupBegin(int groupBegin);
    Q_INVOKABLE int groupBegin() const;
    Q_INVOKABLE void setGroupEnd(int groupEnd);
    Q_INVOKABLE int groupEnd() const;

protected:
    bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;

private:
    TrayIconItemsModel *m_source = nullptr;
    int m_groupBegin = 0;
    int m_groupEnd = 0;
};
}
#endif // STATUSNOTIFIERMODEL_H
